<?php

namespace app\models;

/**
 * This is the model class for table "store_informaction".
 *
 * @property integer $id
 * @property string $store_name
 * @property string $stroe_floor
 * @property string $huodong_content
 * @property integer $ks_time
 * @property integer $js_time
 * @property integer $count
 * @property integer $create_time
 */
class StoreInformaction extends \yii\db\ActiveRecord {
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
    return 'store_informaction';
  }

  /**
   * @inheritdoc
   */
  public function rules()
  {
    return [
        [['huodong_content'], 'string'],
        [['ks_time', 'js_time', 'count', 'create_time'], 'integer'],
        [['store_name'], 'string', 'max' => 80],
        [['stroe_floor'], 'string', 'max' => 10],
    ];
  }

  /**
   * @inheritdoc
   */
  public function attributeLabels()
  {
    return [
        'id'              => 'ID',
        'store_name'      => 'Store Name',
        'stroe_floor'     => 'Stroe Floor',
        'huodong_content' => 'Huodong Content',
        'ks_time'         => 'Ks Time',
        'js_time'         => 'Js Time',
        'count'           => 'Count',
        'create_time'     => 'Create Time',
    ];
  }

  public function getStore($id)
  {
    try {
      $store_info = StoreInformaction::find()->where(['id' => $id])->asArray()->one();
      if ($store_info['count'] == 0) {
        $store_info = StoreInformaction::find()->where(['>','count',0])->asArray()->one();
      }
      return $store_info;
    } catch (\Exception $e) {
      return $e->getMessage();
    }
  }

  public function getBaseStore()
  {
    try {
      $store_info = StoreInformaction::find()->where(['id' => 11])->asArray()->one();
      return $store_info;
    } catch (\Exception $e) {
      return $e->getMessage();
    }
  }

  public function editStore($id)
  {
    try {
      $store_info = StoreInformaction::findOne($id);
      $store_info->count -= 1;
      $store_info->update(0);
      return true;
    } catch (\Exception $e) {
      return $e->getMessage();
    }
  }
}
